/**
 * Created by zn on 17-4-1.
 */
'use strict';
import regular from '../lib/regular'
import user from '../model/user'
import captcha from '../model/captcha'
const userModel = new user()
const captchaModel = new captcha()

module.exports = {
    login: async function(req, res) {
        if (!regular.checkEmail(req.body.email) || !regular.checkPassword(req.body.password) || !regular.checkPlatform(req.body.platform)){
            return res.status(200).json({code: 401})
        }
        try {
            var getUser = await userModel.getUserByEmail(req.body.email)
            if (getUser.length == 0){
                return res.status(200).json({code: 404})
            }
            if(!userModel.checkPassword(getUser[0].pwd, getUser[0].salt, req.body.password)){
                return res.status(200).json({code: 409})
            }
            let token = userModel.createToken(getUser[0].uid, req.body.platform)
            return res.status(200).json({code: 200, uid: getUser[0].uid, token: token})
        } catch(err) {
            return res.status(200).json({code: 402})
        }
    },
    register: async function(req, res) {
        if (!regular.checkEmail(req.body.email) || !regular.checkPassword(req.body.password) || !regular.checkCaptcha(req.body.captcha)){
            return res.status(200).json({code: 401})
        }
        try {
            let getCaptcha = await captchaModel.getCaptcha(req.body.email, 1)
            if(getCaptcha != req.body.captcha){
                return res.status(200).json({code: 407})
            }
            let checkUser = await userModel.getUserByEmail(req.body.email)
            if (checkUser.length != 0){
                return res.status(200).json({code: 403})
            }
            await userModel.addUser(req.body.email, req.body.password)
            captchaModel.delCaptcha(req.body.account)
            return res.status(200).json({code: 200})
        } catch(err) {
            return res.status(200).json({code: 402})
        }
    },
    changePassword: async function(req, res){
        if (!regular.checkEmail(req.body.email) || !regular.checkPassword(req.body.password) || !regular.checkCaptcha(req.body.captcha)){
            return res.status(200).json({code: 401})
        }

        try {
            let getCaptcha = await captchaModel.getCaptcha(req.body.email, 2)
            if(getCaptcha != req.body.captcha){
                return res.status(200).json({code: 407})
            }
            let checkUser = await userModel.getUserByEmail(req.body.email)
            if (checkUser.length == 0){
                return res.status(200).json({code: 403})
            }

            await userModel.editUserPassword(req.body.email, req.body.password)
            captchaModel.delCaptcha(req.body.email, 2)
            userModel.delToken(checkUser[0].uid, 0)

            return res.status(200).json({code: 200})
        } catch(err) {
            return res.status(200).json({code: 402})
        }
    },
    checkToken: async function (req, res){
        if (!regular.checkToken(req.query.token) || !regular.checkUID(req.query.uid) || !regular.checkPlatform(req.query.platform)){
            return res.status(200).json({code: 401})
        }
        try {
            let token = await userModel.getToken(req.query.uid, req.query.platform)
            console.log(req.query.token, token)
            if (token == req.query.token) {
                userModel.updateTokenTime(req.query.uid, req.query.platform)
                return res.status(200).json({code: 200})
            } else {
                return res.status(200).json({code: 410})
            }
        } catch (err) {
            return res.status(200).json({code: 402})
        }
    }
}

